import io from 'socket.io-client'
import store from '@/store'
const socket = io()
socket.on('connect', () => {
  console.log('服务已连接')
})
socket.on('connect', () => {
  store.commit('UPDATE_ISGAMECONNECT', true)
})
socket.on('room_info', ({ nicknames, holder, lines }) => {
  console.log({ nicknames, holder, lines })
  store.commit('SET_NICKNAMES', nicknames)
  store.commit('SET_HOLDER', holder)
  store.commit('SET_LINES', lines)
})
socket.on('user_enter', (nickname) => {
  console.log(nickname, '进入了游戏')
  store.commit('UPDATE_NICKNAMES', nickname)
})
socket.on('user_leave', (nickname) => {
  console.log(nickname, '离开了游戏')
  if (nickname === store.state.holder) {
    store.commit('SET_HOLDER', '')
    console.log('主持人已离开')
  }
  store.commit('EXIT_NICKNAMES', nickname)
})
socket.on('already_started', (holder) => {
  console.log('游戏已开始，主持人是', holder)
  store.commit('SET_HOLDER', holder)
})
socket.on('game_started', (holder) => {
  console.log('游戏已开始，主持人是', holder)
  store.commit('SET_HOLDER', holder)
})
socket.on('game_stoped', () => {
  console.log('游戏结束')
  store.commit('SET_HOLDER', '')
})
socket.on('starting_line', (line) => {
  console.log('开始绘画')
  store.commit('UPDATE_LINES', line)
})
socket.on('updating_line', line => {
  console.log('绘制线条')
  store.commit('UPDATE_LASTlINE', line)
})
socket.on('game_answered', ({ alreadyDone, success, nickname, answer }) => {
  console.log({ alreadyDone, success, nickname, answer })
  if (alreadyDone) {
    store.commit('SET_HOLDER', '')
    console.log('游戏结束')
  }
})
export default socket
